package com.jyniubi.base.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author jyzhang
 *  配置数据库数据新增时createTime和updateTime 自动配置
 *  配置数据库数据时更新时updateTime 自动配置
 */
@Slf4j
@Component
public class GlobalMetaObjectHandler implements MetaObjectHandler {
    // 新增
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ...");
        if (metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime",new Date());
        }
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }
    }

    //更新
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ...");
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }
    }
}
